package cn.tedu.online.examination.system.util;

import java.util.Arrays;
import java.util.Random;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;


public class RandomChack {
	public static String gen() {
		char[] chs = {};
		// char[] chs = {'a','b','c','d','e','f','1','2','3','4','5'
		// ,'A','B','C','D'};
		int num = 0;
		for (int i = 0; i <= '9' - '0'; i++) {
			chs = Arrays.copyOf(chs, chs.length + 1);
			chs[num] = (char) ('0' + i);
			num++;
		}
		for (int i = 0; i <= 'z' - 'a'; i++) {
			chs = Arrays.copyOf(chs, chs.length + 1);
			chs[num] = (char) ('a' + i);
			num++;
		}
		for (int i = 0; i <= 'Z' - 'A'; i++) {
			chs = Arrays.copyOf(chs, chs.length + 1);
			chs[num] = (char) ('A' + i);
			num++;
		}
		// System.out.println(Arrays.toString(chs));
		boolean[] used = new boolean[chs.length];
		char[] nocode = { '1', '0', 'o', 'O', 'I', 'l', 'L', 'Z', '2', '9', 'g' };
		char[] code = new char[4];
		Random r = new Random();
		int i = 0;
		while (true) {
			int index = r.nextInt(chs.length);
			for (int j = 0; j < nocode.length; j++) {
				if (chs[index] == nocode[j]) {
					used[index] = true;
					break;
				}
			}
			if (used[index]) {
				continue;
			}
			code[i++] = chs[index];
			used[index] = true;
			if (i == code.length) {
				break;
			}
		}
		String chack = new String();
		for(int j= 0;j<code.length;j++){
			chack = chack+code[j];
		}

		return chack;
	}
}
